package com.inheritance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inheritance.entity.User;
import com.inheritance.mapper.UserMapper;
import com.inheritance.service.UserService;
import com.inheritance.utils.Md5Util;
import com.inheritance.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    // 查询用户是否存在
    @Override
    public User info(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public int register(User user) {
        user.setPassword(Md5Util.getMD5String(user.getPassword()));
        return userMapper.insert(user);
    }

    @Override
    public User userInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        return userMapper.selectById(map.get("id").toString());
    }

    @Override
    public int delete(String id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int update(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public List<User> list(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getUsername, username);
        queryWrapper.orderByAsc(User::getCreateTime);
        return userMapper.selectList(queryWrapper);
    }
}
